<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class CoachLevel extends \app\BaseModel
{
    protected $name = "massage_service_coach_level";
    public function getOnlineChangeIntegralAttr($value, $data)
    {
        if(isset($value)) {
            return floatval($value);
        }
    }
    public function getAddBasisBalanceAttr($value)
    {
        if(isset($value)) {
            return round($value, 2);
        }
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataInfo($dis)
    {
        $res = $this->where($dis)->find();
        return !empty($res) ? $res->toArray() : [];
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("time_long desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInit($uniacid)
    {
        $info = $this->where(["uniacid" => $uniacid])->order("date_str desc")->find();
        if(empty($info)) {
            $order_model = new Order();
            $start = $order_model->where(["uniacid" => $uniacid])->min("create_time");
            $start = !empty($start) ? $start - 86400 : 0;
        } else {
            $start = $info["date_str"];
        }
        $start = !empty($start) ? $start : time();
        $start = strtotime(date("Y-m-d", $start));
        $eve = strtotime(date("Y-m-d", time())) - $start;
        $eve = $eve / 86400;
        if(!empty($eve)) {
            for ($i = 1; $i <= $eve; $i++) {
                $insert = ["uniacid" => $uniacid, "date" => date("Y-m-d", $start + $i * 86400), "date_str" => $start + $i * 86400];
                $this->dataAdd($insert);
            }
        }
        return true;
    }
    public function getMinTimeLong($caoch_id, $level_cycle, $type = 1)
    {
        $order_model = new Order();
        $dis = ["coach_id" => $caoch_id, "pay_type" => 7];
        if($level_cycle == 1) {
            $week = $type == 1 ? "week" : "last week";
            $price = $order_model->where($dis)->whereTime("create_time", $week)->sum("true_time_long");
        } elseif($level_cycle == 2) {
            $month = $type == 1 ? "month" : "last month";
            $price = $order_model->where($dis)->whereTime("create_time", $month)->sum("true_time_long");
        } elseif($level_cycle == 3) {
            $quarter = $type == 1 ? ceil(date("n") / 3) : ceil(date("n") / 3) - 1;
            $start_quarter = mktime(0, 0, 0, $quarter * 3 - 2, 1, date("Y"));
            $end_quarter = mktime(0, 0, 0, $quarter * 3 + 1, 1, date("Y"));
            $price = $order_model->where($dis)->where("create_time", "between", $start_quarter . "," . $end_quarter)->sum("true_time_long");
        } elseif($level_cycle == 4) {
            $year = $type == 1 ? "year" : "last year";
            $price = $order_model->where($dis)->whereTime("create_time", $year)->sum("true_time_long");
        } elseif($level_cycle == 5) {
            $day = date("d", time());
            if($type == 1) {
                if(15 < $day) {
                    $start_time = strtotime(date("Y-m-16", time()));
                    $end_time = strtotime(date("Y-m-t", time())) + 86399;
                } else {
                    $start_time = strtotime(date("Y-m-01", time()));
                    $end_time = strtotime(date("Y-m-16", time())) - 1;
                }
            } elseif(15 < $day) {
                $start_time = strtotime(date("Y-m-01", time()));
                $end_time = strtotime(date("Y-m-16", time())) - 1;
            } else {
                $start_time = strtotime(date("Y-m-16", strtotime("-1 month")));
                $end_time = strtotime(date("Y-m-t", strtotime("-1 month"))) + 86399;
            }
            $price = $order_model->where($dis)->where("create_time", "between", $start_time . "," . $end_time)->sum("true_time_long");
        } else {
            $price = $order_model->where($dis)->sum("true_time_long");
        }
        return $price;
    }
    public function getMinPrice($caoch_id, $level_cycle, $add = 0, $type = 1, $broker_id = 0)
    {
        $order_model = new Order();
        $dis = ["coach_id" => $caoch_id, "pay_type" => 7];
        if(!empty($broker_id)) {
            $dis["broker_id"] = $broker_id;
        }
        if($add == 1) {
            $dis["is_add"] = $add;
        }
        if($level_cycle == 1) {
            $week = $type == 1 ? "week" : "last week";
            $price = $order_model->where($dis)->whereTime("create_time", $week)->sum("true_service_price");
        } elseif($level_cycle == 2) {
            $month = $type == 1 ? "month" : "last month";
            $price = $order_model->where($dis)->whereTime("create_time", $month)->sum("true_service_price");
        } elseif($level_cycle == 3) {
            $quarter = $type == 1 ? ceil(date("n") / 3) : ceil(date("n") / 3) - 1;
            $start_quarter = mktime(0, 0, 0, $quarter * 3 - 2, 1, date("Y"));
            $end_quarter = mktime(0, 0, 0, $quarter * 3 + 1, 1, date("Y"));
            $price = $order_model->where($dis)->where("create_time", "between", $start_quarter . "," . $end_quarter)->sum("true_service_price");
        } elseif($level_cycle == 4) {
            $year = $type == 1 ? "year" : "last year";
            $price = $order_model->where($dis)->whereTime("create_time", $year)->sum("true_service_price");
        } elseif($level_cycle == 5) {
            $day = date("d", time());
            if($type == 1) {
                if(15 < $day) {
                    $start_time = strtotime(date("Y-m-16", time()));
                    $end_time = strtotime(date("Y-m-t", time())) + 86399;
                } else {
                    $start_time = strtotime(date("Y-m-01", time()));
                    $end_time = strtotime(date("Y-m-16", time())) - 1;
                }
            } elseif(15 < $day) {
                $start_time = strtotime(date("Y-m-01", time()));
                $end_time = strtotime(date("Y-m-16", time())) - 1;
            } else {
                $start_time = strtotime(date("Y-m-16", strtotime("-1 month")));
                $end_time = strtotime(date("Y-m-t", strtotime("-1 month"))) + 86399;
            }
            $price = $order_model->where($dis)->where("create_time", "between", $start_time . "," . $end_time)->sum("true_service_price");
        } else {
            $price = $order_model->where($dis)->sum("true_service_price");
        }
        return round($price, 2);
    }
    public function getCoachOnline($caoch_id, $level_cycle, $type = 1)
    {
        $log_model = new WorkLog();
        //更新在线时长
        $log_model->updateTimeOnline($caoch_id, 1);
        $dis = ["coach_id" => $caoch_id];
//        var_dump($level_cycle);
        if($level_cycle == 1) {
            $week = $type == 1 ? "week" : "last week";
            $time_long = $log_model->where($dis)->whereTime("create_time", $week)->sum("time");
        } elseif($level_cycle == 2) {
            $month = $type == 1 ? "month" : "last month";
            $time_long = $log_model->where($dis)->whereTime("create_time", $month)->sum("time");
        } elseif($level_cycle == 3) {
            $quarter = $type == 1 ? ceil(date("n") / 3) : ceil(date("n") / 3) - 1;
            $start_quarter = mktime(0, 0, 0, $quarter * 3 - 2, 1, date("Y"));
            $end_quarter = mktime(0, 0, 0, $quarter * 3 + 1, 1, date("Y"));
            $time_long = $log_model->where($dis)->where("create_time", "between", $start_quarter . "," . $end_quarter)->sum("time");
        } elseif($level_cycle == 4) {
            $year = $type == 1 ? "year" : "last year";
            $time_long = $log_model->where($dis)->whereTime("create_time", $year)->sum("time");
        } elseif($level_cycle == 5) {
            $day = date("d", time());
            if($type == 1) {
                if(15 < $day) {
                    $start_time = strtotime(date("Y-m-16", time()));
                    $end_time = strtotime(date("Y-m-t", time())) + 86399;
                } else {
                    $start_time = strtotime(date("Y-m-01", time()));
                    $end_time = strtotime(date("Y-m-16", time())) - 1;
                }
            } elseif(15 < $day) {
                $start_time = strtotime(date("Y-m-01", time()));
                $end_time = strtotime(date("Y-m-16", time())) - 1;
            } else {
                $start_time = strtotime(date("Y-m-16", strtotime("-1 month")));
                $end_time = strtotime(date("Y-m-t", strtotime("-1 month"))) + 86399;
            }
            $time_long = $log_model->where($dis)->where("create_time", "between", $start_time . "," . $end_time)->sum("time");
        } else {
            $time_long = $log_model->where($dis)->sum("time");
        }
        $coach_time_model = new CoachTimeList();
        $rest_time_long = $coach_time_model->getCoachRestTimeLong($caoch_id, $level_cycle, $type);
        $time_long = 0 < $time_long - $rest_time_long ? $time_long - $rest_time_long : 0;
        return floor($time_long / 3600);
    }
    public function getMinIntegral($caoch_id, $level_cycle, $type = 1)
    {
        $order_model = new Integral();
        $dis[] = ["coach_id", "=", $caoch_id];
        $dis[] = ["status", "=", 1];
        $dis[] = ["type", "in", [0, 2]];
        if($level_cycle == 1) {
            $week = $type == 1 ? "week" : "last week";
            $price = $order_model->where($dis)->whereTime("create_time", $week)->sum("integral");
        } elseif($level_cycle == 2) {
            $month = $type == 1 ? "month" : "last month";
            $price = $order_model->where($dis)->whereTime("create_time", $month)->sum("integral");
        } elseif($level_cycle == 3) {
            $quarter = $type == 1 ? ceil(date("n") / 3) : ceil(date("n") / 3) - 1;
            $start_quarter = mktime(0, 0, 0, $quarter * 3 - 2, 1, date("Y"));
            $end_quarter = mktime(0, 0, 0, $quarter * 3 + 1, 1, date("Y"));
            $price = $order_model->where($dis)->where("create_time", "between", $start_quarter . "," . $end_quarter)->sum("integral");
        } elseif($level_cycle == 4) {
            $year = $type == 1 ? "year" : "last year";
            $price = $order_model->where($dis)->whereTime("create_time", $year)->sum("integral");
        } elseif($level_cycle == 5) {
            $day = date("d", time());
            if($type == 1) {
                if(15 < $day) {
                    $start_time = strtotime(date("Y-m-16", time()));
                    $end_time = strtotime(date("Y-m-t", time())) + 86399;
                } else {
                    $start_time = strtotime(date("Y-m-01", time()));
                    $end_time = strtotime(date("Y-m-16", time())) - 1;
                }
            } elseif(15 < $day) {
                $start_time = strtotime(date("Y-m-01", time()));
                $end_time = strtotime(date("Y-m-16", time())) - 1;
            } else {
                $start_time = strtotime(date("Y-m-16", strtotime("-1 month")));
                $end_time = strtotime(date("Y-m-t", strtotime("-1 month"))) + 86399;
            }
            $price = $order_model->where($dis)->where("create_time", "between", $start_time . "," . $end_time)->sum("integral");
        } else {
            $price = $order_model->where($dis)->sum("integral");
        }
        return $price;
    }
    public function initTop($uniacid)
    {
        $dis = ["uniacid" => $uniacid, "status" => 1];
        $list = $this->where($dis)->order("time_long,id desc")->select()->toArray();
        $key = "coach_level_key";
        incCache($key, 1, $this->_uniacid);
        $key_value = getCache($key, $this->_uniacid);
        if($key_value == 1 && !empty($list)) {
            foreach ($list as $k => $value) {
                $this->dataUpdate(["id" => $value["id"]], ["top" => $k + 1]);
            }
        }
        decCache($key, 1, $this->_uniacid);
        return true;
    }
    public function getMinCount($caoch_id, $level_cycle, $add = 0, $type = 1)
    {
        $order_model = new Order();
        $dis = ["coach_id" => $caoch_id, "pay_type" => 7];
        if($add == 1) {
            $dis["is_add"] = $add;
        }
        if($level_cycle == 1) {
            $week = $type == 1 ? "week" : "last week";
            $price = $order_model->where($dis)->whereTime("create_time", $week)->count();
        } elseif($level_cycle == 2) {
            $month = $type == 1 ? "month" : "last month";
            $price = $order_model->where($dis)->whereTime("create_time", $month)->count();
        } elseif($level_cycle == 3) {
            $quarter = $type == 1 ? ceil(date("n") / 3) : ceil(date("n") / 3) - 1;
            $start_quarter = mktime(0, 0, 0, $quarter * 3 - 2, 1, date("Y"));
            $end_quarter = mktime(0, 0, 0, $quarter * 3 + 1, 1, date("Y"));
            $price = $order_model->where($dis)->where("create_time", "between", $start_quarter . "," . $end_quarter)->count();
        } elseif($level_cycle == 4) {
            $year = $type == 1 ? "year" : "last year";
            $price = $order_model->where($dis)->whereTime("create_time", $year)->count();
        } elseif($level_cycle == 5) {
            $day = date("d", time());
            if($type == 1) {
                if(15 < $day) {
                    $start_time = strtotime(date("Y-m-16", time()));
                    $end_time = strtotime(date("Y-m-t", time())) + 86399;
                } else {
                    $start_time = strtotime(date("Y-m-01", time()));
                    $end_time = strtotime(date("Y-m-16", time())) - 1;
                }
            } elseif(15 < $day) {
                $start_time = strtotime(date("Y-m-01", time()));
                $end_time = strtotime(date("Y-m-16", time())) - 1;
            } else {
                $start_time = strtotime(date("Y-m-16", strtotime("-1 month")));
                $end_time = strtotime(date("Y-m-t", strtotime("-1 month"))) + 86399;
            }
            $price = $order_model->where($dis)->where("create_time", "between", $start_time . "," . $end_time)->count();
        } else {
            $price = $order_model->where($dis)->count();
        }
        return $price;
    }
    public function getCoachStar($caoch_id, $level_cycle, $type = 1)
    {
        $order_model = new Comment();
        $dis[] = ["coach_id", "=", $caoch_id];
        $dis[] = ["status", ">", -1];
        if($level_cycle == 1) {
            $week = $type == 1 ? "week" : "last week";
            $price = $order_model->where($dis)->whereTime("create_time", $week)->avg("star");
        } elseif($level_cycle == 2) {
            $month = $type == 1 ? "month" : "last month";
            $price = $order_model->where($dis)->whereTime("create_time", $month)->avg("star");
        } elseif($level_cycle == 3) {
            $quarter = $type == 1 ? ceil(date("n") / 3) : ceil(date("n") / 3) - 1;
            $start_quarter = mktime(0, 0, 0, $quarter * 3 - 2, 1, date("Y"));
            $end_quarter = mktime(0, 0, 0, $quarter * 3 + 1, 1, date("Y"));
            $price = $order_model->where($dis)->where("create_time", "between", $start_quarter . "," . $end_quarter)->avg("star");
        } elseif($level_cycle == 4) {
            $year = $type == 1 ? "year" : "last year";
            $price = $order_model->where($dis)->whereTime("create_time", $year)->avg("star");
        } elseif($level_cycle == 5) {
            $day = date("d", time());
            if($type == 1) {
                if(15 < $day) {
                    $start_time = strtotime(date("Y-m-16", time()));
                    $end_time = strtotime(date("Y-m-t", time())) + 86399;
                } else {
                    $start_time = strtotime(date("Y-m-01", time()));
                    $end_time = strtotime(date("Y-m-16", time())) - 1;
                }
            } elseif(15 < $day) {
                $start_time = strtotime(date("Y-m-01", time()));
                $end_time = strtotime(date("Y-m-16", time())) - 1;
            } else {
                $start_time = strtotime(date("Y-m-16", strtotime("-1 month")));
                $end_time = strtotime(date("Y-m-t", strtotime("-1 month"))) + 86399;
            }
            $price = $order_model->where($dis)->where("create_time", "between", $start_time . "," . $end_time)->avg("star");
        } else {
            $price = $order_model->where($dis)->avg("star");
        }
        return !empty($price) ? round($price, 1) : 5;
    }
}

?>